package com.ug_project.adapters

import android.app.Activity
import android.content.Context
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.chad.library.adapter.base.module.LoadMoreModule

import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.ug_project.objects.isNull
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

/**
 * Created by yeganaaa on 3/7/2017.
 */

class RecyclerMultiTypeAdapter<T:MultiItemEntity>(
    val _context: Context?, var
    defaultlayout_resID: Int,
    var datas: ArrayList<T>
) : BaseMultiItemQuickAdapter<T, BaseViewHolder>(datas), LoadMoreModule {

    var basedata = ArrayList<T>()
    private var _GetItemViewType: ((position: Int) -> Int) = { defaultlayout_resID }
    private var _OnBindViewHolder: ((itemview: View, itemViewResID: Int, data: ArrayList<T>, position: Int) -> Unit)? =
        null
    private var _OnBindViewHolderForItemData: ((view: View, itemViewResID: Int, data: T, position: Int) -> Unit)? =
        null
    private var _OnViewRecycled: ((BaseViewHolder) -> Unit) = {}
    private var _inflater: LayoutInflater? = null


    init {
        animationEnable = true
        basedata.addAll(datas)
        _inflater = LayoutInflater.from(context)
    }

    private fun checkInflater(){
        _inflater.isNull {
            _inflater = LayoutInflater.from(context)
        }
    }

    open fun addHeader(layoutRedId: Int, action: (view: View) -> View) {
        //checkInflater()
        val headerView = _inflater?.inflate(layoutRedId, null, false)
        addHeaderView(action(headerView!!))
    }

    open fun addFooter(layoutRedId: Int, action: (view: View) -> View) {
        //checkInflater()
        val footerView = _inflater?.inflate(layoutRedId, null, false)

        addFooterView(action(footerView!!))
    }

    fun filter(action: (T) -> Boolean) {
        datas.clear()
        datas.addAll(basedata.filter { action(it) })
        notifyDataSetChanged()
    }


    open fun GetItemViewType(getviewtype: (position: Int) -> Int) {
        _GetItemViewType = getviewtype
    }


    open fun OnBindViewHolder(binder: (rootView: View, itemViewResID: Int, data: ArrayList<T>, position: Int) -> Unit) {
        _OnBindViewHolder = binder
    }

    /**
     * On Bind View Holder For Item Data
     */
    open fun OnBindViewHolderIData(binder: (view: View, itemViewResID: Int, data: T, position: Int) -> Unit) {
        _OnBindViewHolderForItemData = binder
    }



    override fun convert(holder: BaseViewHolder, item: T) {
        _OnBindViewHolderForItemData?.invoke(
            holder.itemView,
            defaultlayout_resID,
            item,
            holder.adapterPosition
        )
    }

    override fun convert(holder: BaseViewHolder, item: T, payloads: List<Any>) {
        super.convert(holder, item, payloads)
    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)
        _OnViewRecycled.invoke(holder)
    }

    open fun onViewRecycled(action: (BaseViewHolder) -> Unit) {
        _OnViewRecycled = action
    }

    //********************* LoadMore ********************
    open fun onLoadingMore(action: () -> Unit) {
        loadMoreModule.setOnLoadMoreListener {
            action()
        }
    }

    open fun loadingMoreComplete() {
        loadMoreModule.loadMoreComplete()
    }

    open fun loadingMoreEnd(gone:Boolean) {
        loadMoreModule.loadMoreEnd(gone)
    }

    open fun loadingMoreFail() {
        loadMoreModule.loadMoreFail()
    }

    open fun loadingMore() {
        loadMoreModule.loadMoreToLoading()
    }


    fun refresh() {
        basedata.clear()
        basedata.addAll(datas)
        notifyDataSetChanged()
    }


}




//fun <T> Activity.createRecyclerAdapter(
//    defaultlayout_resID: Int, data: ArrayList<T>,
//    creator: RecyclerMultiTypeAdapter<T>.() -> Unit
//): RecyclerMultiTypeAdapter<T> {
//    return createRecyclerAdapter(this, defaultlayout_resID, data, creator)
//}

